#include "blockqueue.hpp"
#include <iostream>
#include <pthread.h>
#include <string>
#include <ctime>
#include <unistd.h>
#include <cstring>
#include "task.hpp"
using namespace std;
void* customerrun(void* arg)
{       
    //BlockQueue<int>* bq=static_cast<BlockQueue<int>*>(arg);
    BlockQueue<Task>* bq=static_cast<BlockQueue<Task>*>(arg);
    while(true)
    {
        //sleep(1);
        //int x=0;
        //bq->pop(&x);
        //cout<<"customer val->"<<x<<endl;

        sleep(3);
        Task t;
        bq->pop(&t);
        t();
        cout<<t.to_customer()<<endl;
    }
    return nullptr;

}


void* productorrun(void* arg)
{
    //BlockQueue<int>* bq=static_cast<BlockQueue<int>*>(arg);
    BlockQueue<Task>* bq=static_cast<BlockQueue<Task>*>(arg);
    while(true)
    {
        //sleep(1);
        //int x=rand()%100;
        //bq->push(x);
        //cout<<"productor val->"<<x<<endl;

        //sleep(1);
        char arr[]="+-*%/";
        char c=arr[rand()%strlen(arr)];
        int x=rand()%20;
        int y=rand()%10;
        Task t(x,y,c);
        bq->push(t);
        cout<<t.to_productor()<<endl;
    }
    return nullptr;
}

int main()
{
    srand((uint64_t)time(0));
    pthread_t customer[3],productor[3];
    //BlockQueue<int>* bq=new BlockQueue<int>();
    BlockQueue<Task>* bq=new BlockQueue<Task>();
    pthread_create(&customer[0],nullptr,customerrun,bq);
    pthread_create(&customer[1],nullptr,customerrun,bq);
    pthread_create(&customer[2],nullptr,customerrun,bq);
    pthread_create(&productor[0],nullptr,productorrun,bq);
    pthread_create(&productor[1],nullptr,productorrun,bq);
    pthread_create(&productor[2],nullptr,productorrun,bq);
    pthread_join(customer[0],nullptr);
    pthread_join(customer[1],nullptr);
    pthread_join(customer[2],nullptr);
    pthread_join(productor[0],nullptr);
    pthread_join(productor[1],nullptr);
    pthread_join(productor[2],nullptr);
    return 0;
}
